// pages/cityList/cityList.js
const app = getApp();
var api=require("../../api/api");
var home=require("../../api/home");
var util=require("../../utils/util");

Page({
  data: {
    city: '佛山市',
    cityList: [],
    list: [],
    height: wx.getSystemInfoSync().windowHeight,
    hotlist: [
      {"cityId": 1,"provincecode":"110000","cityName":"北京市","lat": "39.904987","lng": "116.405289"},
      {"cityId": 205,"provincecode":"440000","cityName":"佛山市","lat": "23.028763","lng": "113.122719"},
      {"cityId": 202,"provincecode":"440000","cityName":"深圳市","lat": "22.547001","lng": "114.085945"},
      {"cityId": 73,"provincecode":"310000","cityName":"上海市","lat": "31.231707","lng": "121.472641"},
      {"cityId": 200,"provincecode":"440000","cityName":"广州市","lat": "23.125177","lng": "113.28064"},
      {"cityId": 2,"provincecode":"120000","cityName":"天津市","lat": "39.125595","lng": "117.190186"}
    ],
    nav_bar: 0,
    boxTop: 0,
    hide: true,
    word: 'A'
  },
  onLoad:function(options){
    var that = this;
    var list = [{}],cityList=[{}];
    for (let i = 0; i < 26; i++) {
        list[i] = {};
        list[i].name = String.fromCharCode(65 + i);
        cityList[i]={};
        cityList[i].initial=String.fromCharCode(65 + i);
        cityList[i].cityInfo=[]
    }
    that.setData({list: list,cityList:cityList,city: options.city})
    that.get_all_city_list();
  },
  //重新定位
  choose_location:function(){
    var that=this;
    wx.getSetting({
      success: function (res) {
        if(res.authSetting["scope.userLocation"]){
          that.change_location()
        }else{
          // 重新授权
          wx.openSetting({
            success:function(res){
              if (res.authSetting["scope.userLocation"]) {
                that.change_location()
              }
            }
          })
        }
      }
    })
  },
  change_location:function(){
    var that=this;
    wx.chooseLocation({
      success:function (res) {
        // console.log(res)
        var latitude=res.latitude,longitude=res.longitude;
        var pages = getCurrentPages();
        var prevPage = pages[pages.length - 2];
        prevPage.setData({check_loca:true,latitude:latitude,longitude:longitude})
        that.setData({city:res.name})
        setTimeout(() => {
          wx.navigateBack({
            delta: 1
          })
        }, 1000);
      },
      fail:function(res){
        util.page_show_toast('重新定位失败',1500)
      }
    })
  },
  // 滚动联动
  scroll_top: function (e) {
    var that = this;
    var scroll_top = e.detail.scrollTop;
    var top_arr = that.data.top_arr;
    for (var i = 0; i < top_arr.length; i++) {
      if ((i + 1) < top_arr.length) {
        if (scroll_top >= parseInt(top_arr[i].top) && scroll_top < parseInt(top_arr[i + 1].top)) {
          that.setData({ nav_bar: i })
        }
        if (scroll_top >= top_arr[top_arr.length - 1].top) {
          that.setData({ nav_bar: top_arr.length - 1 })
        }
      }
    }
  },
  // 字母联动
  getCur: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var nav_bar = that.data.nav_bar,list = that.data.list;
    that.setData({word: list[index].name});
    if (nav_bar == index) {
      return;
    } else {
      that.setData({ nav_bar: index, to_view: 'city' + index.toString() })
    }
  },
  setCur: function(e){
    // console.log(e)
  },
  tStart:function(e){
    // console.log(e)
    this.setData({hide: false})
  },
  tEnd:function(e){
    // console.log(e)
    this.setData({hide: true})
  },
  tMove:function(e){
    // console.log(e)
    var that = this,list = that.data.list;
    let num = parseInt((e.touches[0].clientY - that.data.boxTop) / 20);
    if (e.touches[0].clientY > that.data.boxTop && num>0 && num<26) {
      that.setData({ nav_bar: num, to_view: 'city' + (num+1),word: list[num].name })
    }
  },
  // 选择城市
  check_city:function(e){
    var that=this;
    var item=e.currentTarget.dataset.item;
    that.setData({city:item.cityName})
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];
    prevPage.setData({city: item.cityName,check_loca:true,latitude:item.lat,longitude:item.lng})
    setTimeout(() => {
      wx.navigateBack({
        delta: 1
      })
    }, 800);
  },
  //
  check_ready: function () {
    var that = this;
    var query = wx.createSelectorQuery();
    query.selectAll('.check_city').boundingClientRect(function (res) {
      // console.log(res)
      that.setData({ top_arr: res })
    }).exec()
    query.selectAll('.bar_box').boundingClientRect(function (res) {
      // console.log(res)
      that.setData({ boxTop: res[0].top })
    }).exec()
  },
  // 获取城市
  get_all_city_list:function(){
    var that=this,cityList=that.data.cityList;
    wx.showLoading({title: '加载中'})
    util.baseRequest({
      url: api.data.requestUrl + api.data.getAllCityList,
      method: 'GET',
    }).then(res=>{
      wx.hideLoading()
      var lists=res.data;
      for(var i=0;i<lists.length;i++){
        for(var j=0;j<cityList.length;j++){
          if(cityList[j].initial==lists[i].initials){
            cityList[j].cityInfo.push(lists[i])
          }
        }
      }
      that.setData({cityList: cityList})
      that.check_ready();
    }).catch(error=>{
      console.log(error)
      wx.hideLoading()
    })
  },
})